/**
 * 
 */
package hr.fer.zemris.annotator.gui;

import hr.fer.zemris.annotator.PerformanceAnnotator;
import hr.fer.zemris.annotator.model.ApplicationParameters;
import hr.fer.zemris.annotator.model.interfaces.IApplicationParameters;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author dajan
 * 
 */
public class ApplicationParametersView extends JFrame implements
		ActionListener, IApplicationParameters {

	private JFileChooser compiledPathChooser, outputPathChooser,
			sourcePathChooser;
	private JButton openCompiledProgramButton, selectOutputFolderButton,
			selectSourceFolderButton, runButton;
	private JLabel openCompiledProgramLabel, selectOutputFolderLabel,
			selectSourceLabel, openCompiledProgramLabelShow,
			selectOutputFolderLabelShow, selectSourceLabelShow;

	public ApplicationParametersView() {

		initApplicationSettingsView();
	}

	private void initApplicationSettingsView() {

		setTitle("Performan(C)eAnnotator");
		setSize(300, 250);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel mainPanel = new JPanel();
		JPanel compiledPanel = new JPanel();
		JPanel outputPanel = new JPanel();
		JPanel sourcePanel = new JPanel();
		JPanel labelShowPanel1 = new JPanel();
		JPanel labelShowPanel2 = new JPanel();
		JPanel labelShowPanel3 = new JPanel();
		JPanel runPanel = new JPanel();
		FlowLayout flowLayout = new FlowLayout();

		flowLayout.setAlignment(FlowLayout.TRAILING);

		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		compiledPanel.setLayout(flowLayout);
		outputPanel.setLayout(flowLayout);
		sourcePanel.setLayout(flowLayout);

		compiledPathChooser = new JFileChooser();
		outputPathChooser = new JFileChooser();
		sourcePathChooser = new JFileChooser();

		sourcePathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		outputPathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		openCompiledProgramButton = new JButton("Select program");
		selectOutputFolderButton = new JButton("Select directory");
		selectSourceFolderButton = new JButton("Select directory");
		runButton = new JButton("Run Performan(C)e Annotator");

		openCompiledProgramButton.addActionListener(this);
		selectOutputFolderButton.addActionListener(this);
		selectSourceFolderButton.addActionListener(this);
		runButton.addActionListener(this);

		openCompiledProgramLabel = new JLabel("Open compiled program: ");
		selectOutputFolderLabel = new JLabel("Select output directory: ");
		selectSourceLabel = new JLabel("Select source directory: ");

		openCompiledProgramLabelShow = new JLabel();
		selectOutputFolderLabelShow = new JLabel();
		selectSourceLabelShow = new JLabel();

		openCompiledProgramLabelShow.setAlignmentX(LEFT_ALIGNMENT);

		openCompiledProgramLabelShow.setVisible(false);
		selectOutputFolderLabelShow.setVisible(false);
		selectSourceLabelShow.setVisible(false);

		compiledPanel.add(openCompiledProgramLabel);
		compiledPanel.add(openCompiledProgramButton);

		outputPanel.add(selectOutputFolderLabel);
		outputPanel.add(selectOutputFolderButton);

		sourcePanel.add(selectSourceLabel);
		sourcePanel.add(selectSourceFolderButton);

		labelShowPanel1.add(openCompiledProgramLabelShow);
		labelShowPanel2.add(selectSourceLabelShow);
		labelShowPanel3.add(selectOutputFolderLabelShow);
		runPanel.add(runButton);

		mainPanel.add(compiledPanel);
		mainPanel.add(labelShowPanel1);
		mainPanel.add(sourcePanel);
		mainPanel.add(labelShowPanel2);
		mainPanel.add(outputPanel);
		mainPanel.add(labelShowPanel3);
		mainPanel.add(runPanel);

		getContentPane().add(mainPanel);

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		// handle openCompiledPrograButton
		if (e.getSource() == openCompiledProgramButton) {
			int returnVal = compiledPathChooser
					.showOpenDialog(ApplicationParametersView.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = compiledPathChooser.getSelectedFile();

				setCompiledProgramPath(file.getParent());
				setCompiledProgramName(file.getName());
				openCompiledProgramLabelShow.setText(getCompiledProgramPath());
				openCompiledProgramLabelShow.setVisible(true);

			} else {
				// log.append("Open command cancelled by user." + newline);
			}

		}

		// Handle selectSourceFolderButton
		if (e.getSource() == selectSourceFolderButton) {
			int returnVal = sourcePathChooser
					.showOpenDialog(ApplicationParametersView.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = sourcePathChooser.getSelectedFile();

				setSourcePath(file.getAbsolutePath());
				selectSourceLabelShow.setText(getSourcePath());
				selectSourceLabelShow.setVisible(true);

			} else {

			}

		}

		// Handle selectSourceFolderButton
		if (e.getSource() == selectOutputFolderButton) {
			int returnVal = outputPathChooser
					.showOpenDialog(ApplicationParametersView.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = outputPathChooser.getSelectedFile();

				setOutputPath(file.getAbsolutePath());
				selectOutputFolderLabelShow.setText(getOutputPath());
				selectOutputFolderLabelShow.setVisible(true);

			} else {

			}

		}

		if (e.getSource() == runButton) {

			String returnStatus = PerformanceAnnotator
					.runPerformanceAnnotator();

			// finish the program in the end..
			System.exit(1);

		}

	}

	@Override
	public void setCompiledProgramPath(String path) {

		ApplicationParameters.getInstance().setCompiledProgramPath(path);

	}

	@Override
	public void setSourcePath(String path) {

		ApplicationParameters.getInstance().setSourcePath(path);

	}

	@Override
	public void setOutputPath(String path) {

		ApplicationParameters.getInstance().setOutputPath(path);

	}

	@Override
	public String getCompiledProgramPath() {

		return ApplicationParameters.getInstance().getCompiledProgramPath();
	}

	@Override
	public String getSourcePath() {

		return ApplicationParameters.getInstance().getSourcePath();
	}

	@Override
	public String getOutputPath() {

		return ApplicationParameters.getInstance().getOutputPath();
	}

	@Override
	public void setCompiledProgramName(String programName) {

		ApplicationParameters.getInstance().setCompiledProgramName(programName);

	}

	@Override
	public String getCompiledProgramName() {

		return ApplicationParameters.getInstance().getCompiledProgramName();

	}
}
